<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>前端使用 jest 做单元测试 | sunJsona的个人博客</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1. 安装 Jest1npm install --save-dev jest 2. 在script里面加上下面一句：123456/ 添加测试命令&amp;#123;  &amp;quot;scripts&amp;quot;: &amp;#123;    &amp;quot;test&amp;quot;: &amp;quot;jest&amp;quot;  &amp;#125;&amp;#125; 3. 添加被测试文件12345// 被测试文件 sum.jsfunction s">
<meta property="og:type" content="article">
<meta property="og:title" content="前端使用 jest 做单元测试">
<meta property="og:url" content="https://xiaochangzai.github.io/2020/07/07/前端使用 jest 做单元测试/index.html">
<meta property="og:site_name" content="sunJsona的个人博客">
<meta property="og:description" content="1. 安装 Jest1npm install --save-dev jest 2. 在script里面加上下面一句：123456/ 添加测试命令&amp;#123;  &amp;quot;scripts&amp;quot;: &amp;#123;    &amp;quot;test&amp;quot;: &amp;quot;jest&amp;quot;  &amp;#125;&amp;#125; 3. 添加被测试文件12345// 被测试文件 sum.jsfunction s">
<meta property="og:locale" content="Simple Chinese">
<meta property="og:updated_time" content="2020-07-07T05:00:13.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="前端使用 jest 做单元测试">
<meta name="twitter:description" content="1. 安装 Jest1npm install --save-dev jest 2. 在script里面加上下面一句：123456/ 添加测试命令&amp;#123;  &amp;quot;scripts&amp;quot;: &amp;#123;    &amp;quot;test&amp;quot;: &amp;quot;jest&amp;quot;  &amp;#125;&amp;#125; 3. 添加被测试文件12345// 被测试文件 sum.jsfunction s">
  
    <link rel="alternative" href="/atom.xml" title="sunJsona的个人博客" type="application/atom+xml">
  
  
    <link rel="icon" href="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg">
  
  <link rel="stylesheet" href="/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">sunJsona</a></h1>
		</hgroup>

		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/">主页</a></li>
				        
							<li><a href="/archives">所有文章</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">sunJsona</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">sunJsona</h1>
			</hgroup>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/">主页</a></li>
		        
					<li><a href="/archives">所有文章</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-前端使用 jest 做单元测试" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2020/07/07/前端使用 jest 做单元测试/" class="article-date">
  	<time datetime="2020-07-07T05:00:13.000Z" itemprop="datePublished">2020-07-07</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      前端使用 jest 做单元测试
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="1-安装-Jest"><a href="#1-安装-Jest" class="headerlink" title="1. 安装 Jest"></a>1. 安装 Jest</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save-dev jest</span><br></pre></td></tr></table></figure>
<h3 id="2-在script里面加上下面一句："><a href="#2-在script里面加上下面一句：" class="headerlink" title="2. 在script里面加上下面一句："></a>2. 在script里面加上下面一句：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">/ 添加测试命令</span><br><span class="line">&#123;</span><br><span class="line">  &quot;scripts&quot;: &#123;</span><br><span class="line">    &quot;test&quot;: &quot;jest&quot;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-添加被测试文件"><a href="#3-添加被测试文件" class="headerlink" title="3. 添加被测试文件"></a>3. 添加被测试文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 被测试文件 sum.js</span><br><span class="line">function sum(a, b) &#123;</span><br><span class="line">  return a + b;</span><br><span class="line">&#125;</span><br><span class="line">module.exports = sum;</span><br></pre></td></tr></table></figure>
<h3 id="4-书写测试文件"><a href="#4-书写测试文件" class="headerlink" title="4. 书写测试文件"></a>4. 书写测试文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 测试文件 sum.test.js</span><br><span class="line">const sum = require(‘./sum&apos;);</span><br><span class="line">test(&apos;adds 1 + 2 to equal 3&apos;, () =&gt; &#123;</span><br><span class="line">  expect(sum(1, 2)).toBe(3);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="5-控制台执行"><a href="#5-控制台执行" class="headerlink" title="5. 控制台执行"></a>5. 控制台执行</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm test</span><br><span class="line">// 或者</span><br><span class="line">npm t</span><br></pre></td></tr></table></figure>
<p><a href="https://blog.csdn.net/u011818572/article/details/81638872" target="_blank" rel="noopener">vue+typescript+jest测试指南</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/07/07/判断手机系统是ios 还是Android/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          判断手机系统是ios 还是Android
        
      </div>
    </a>
  
  
    <a href="/2020/07/07/在html代码里添加关键字、描述/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">在html代码里添加关键字、描述</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="前端使用 jest 做单元测试" data-title="前端使用 jest 做单元测试" data-url="https://xiaochangzai.github.io/2020/07/07/前端使用 jest 做单元测试/"  data-images="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" data-content="前端使用 jest 做单元测试">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2022 sunJsona
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>